<script lang="ts">
export default {
  name: 'audioList'
};
</script>

<script setup lang="ts">
import router from '@/router';
import AudioEdit from './components/audioEdit.vue';
import { formatMillisecondsToDate } from '@/utils/utils'
import {
  updateVoice,
  addVoice,
  getVoiceList,
  deleteListVoice,
  updateStateVoice,
  getVoiceById,
} from '@/api/audio';
import {
  getDeviceList
} from '@/api/device';
import{getScreenList}from "@/api/screen"
import { getDictPage } from '@/api/dict'

const queryFormRef = ref(ElForm); // 查询表单
const formRef = ref(ElForm); // 用户表单
const defaultTime: [Date, Date] = [
  new Date(2000, 1, 1, 0, 0, 0),
  new Date(2000, 2, 1, 23, 59, 59)
];
const loading = ref(false);
const ids = ref<number[]>([]);
const total = ref(0);

const statusList = [
  {name: '开', value: 1},
  {name: '关', value: -1}
]

const commandList = ref<any>([]);

const deviceList = ref<any>([]);

const pptList = ref<any>([]);

const sourceList = ref<any>([]);
const queryParams: any = reactive({
  current: 1,
  pageSize: 10,
  voiceWord: "",
  title: "",
  actionShow:'',
  state: 0,
});

const formData = reactive({
  voiceWord: "",
  title: "",  //设备名称/资源名称
  contentId:'', //资源id  设备id
  sourceType:'', //1 资源 6设备
  actionSend:"", //区域的name
  actionShow:"", //区域的value
});

const dialog = reactive<any>({
  visible: false,
});

const dataList = ref([]);

/**
 * 查询
 */
function handleQuery() {
  getVoiceList(queryParams)
    .then((res: any) => {
      dataList.value = res.data.data;
      total.value = res.data.total;
    })
}
/**
 * 重置查询
 */
function resetQuery() {
  queryFormRef.value.resetFields();
  queryParams.current = 1;
  handleQuery();
}

/**
 * 字典查询
 */
function getDict() {
  //指令动作 155
  getDictPage({
    current: 1,
    pageSize: 9999,
    parentId:155
  }).then((res) => {
    let adata = res.data.data
    commandList.value = adata
  }) 
  //应用资源类型157
  /**
   * 6、7物联网设备
   * 其他是数字资源
   */
  getDictPage({
    current: 1,
    pageSize: 9999,
    parentId:157
  }).then((res) => {
    let adata = res.data.data
    sourceList.value = adata
  })
}

/**
 * 设备查询
 */
 function getProduct() {
  getDeviceList({
    "current": 1,
    "pageSize": 9999,
    name:"",
    description:"",
    area:"",
    addressCode:"",
    state:0,
    productId:0
  }).then((res) => {
    let adata:any = res.data.data
    // deviceList.value = JSON.parse(JSON.stringify(res.data.data));
    deviceList.value = adata
  })
}
// 获取所有资源getPptList
function getPptLists() {
  getScreenList({
    "current": 1,
    "pageSize": 9999,
    "screen_id":''
  }).then((res) => {
    let adata:any = res.data.data
    console.log('资源列表==',adata)
    // deviceList.value = JSON.parse(JSON.stringify(res.data.data));
    pptList.value = adata
  })
}

/**
 * 行checkbox change事件
 */
function handleSelectionChange(selection: any) {
  ids.value = selection.map((item: any) => item.id);
}

//修改状态
function updateState(id: number, state: number) {
  // state == 1 ? state = -1 : state = 1
  updateStateVoice({ id, state }).then(() => {
    ElMessage.success('修改成功');
    //handleQuery();
  });
}

/**
 * 重置表单
 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
}


/**
 * 类型表单弹窗
 *
 */
function openDialog(row?: object) {
  dialog.visible = true;
  if (row) {
    dialog.title = '修改语义控制';
    Object.assign(formData, row);
  } else {
    //resetForm();
    dialog.title = '新增语义控制';
  }
}

/**
 * 删除
 */
function handleDelete(id?: number) {
  let idList = ids.value
  id ? idList = [...ids.value, id] : null
  if (idList.length === 0) {
    ElMessage.warning('请勾选删除项');
    return;
  }

  ElMessageBox.confirm('确认删除已选中的数据项?', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    deleteListVoice({ ids: idList }).then(() => {
      ElMessage.success('删除成功');
      handleQuery();
    });
  });
}

onMounted(async () => {
  getDict(); // 初始化字典
  getProduct(); // 初始化产品列表
  getPptLists(); // 初始化ppt列表
  handleQuery(); // 初始化用户列表数据
});

watch(
  () => queryParams.startEndTime,
  (val: any) => {
    if (Array.isArray(val)) {
      queryParams.beginTime = val[0];
      queryParams.endTime = val[1];
    } else {
      queryParams.beginTime = '';
      queryParams.endTime = '';
    }
  }
);


</script>

<template>
  <div class="app-container">

    <el-row :gutter="24">
      <el-col :lg="24" :xs="24">
        <div class="search">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">

            <el-form-item prop="voiceWord" label="语义输入">
              <el-input v-model="queryParams.voiceWord" placeholder="请输入语义关键词" />
            </el-form-item>
            <el-form-item prop="title" label="指令对象">
              <el-input v-model="queryParams.title" placeholder="请输入指令对象关键字" />
            </el-form-item>
          <el-form-item prop="actionSend" label="指令动作">
            <el-select v-model="queryParams.actionSend" placeholder="请选择">
              <el-option v-for="item in commandList" :key="item.name" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item>

            <el-form-item prop="state" label="状态">
              <el-select v-model="queryParams.state" placeholder="请选择">
                <el-option v-for="item in statusList" :key="item.name" :label="item.name" :value="item.value" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleQuery">
                <i-ep-search />搜索
              </el-button>
              <el-button @click="resetQuery">
                <i-ep-refresh />
                重置
              </el-button>
            </el-form-item>
          </el-form>
        </div>

        <el-card shadow="never">
          <template #header>
            <div class="flex justify-between">
              <div>
                <el-button type="success" @click="openDialog()">
                  <i-ep-plus />新增
                </el-button>
              </div>
            </div>
          </template>

          <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
            <el-table-column type="index" width="100" />
            <el-table-column label="语义输入" align="center" prop="voiceWord" />
            <el-table-column label="指令对象" align="center" prop="title" />
            <el-table-column label="指令动作" align="center" prop="actionSend" >
              <template #default="scope">
                {{ commandList.find(item => item.value === scope.row.actionSend)?.name }}  
              </template>
            </el-table-column>
            <el-table-column label="状态" fixed="right" align="left" width="200">
              <template #default="scope">
                <!-- {{ scope.row.state }} -->
                <el-switch v-model="scope.row.state" :inactive-value="-1" :active-value="1" 
                  @click="updateState(scope.row.id, scope.row.state)" />
              </template>
            
            </el-table-column>
        

            <!-- <el-table-column label="描述" align="center" prop="description" /> -->
            <el-table-column label="操作" fixed="right" align="left" width="200">
              <template #default="scope">
                <el-button type="primary" link size="small" @click.stop="openDialog(scope.row)">
                  <i-ep-edit />编辑
                </el-button>
                <el-button type="primary" link size="small" @click.stop="handleDelete(scope.row.id)">
                  <i-ep-delete />删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
    </el-row>
    <pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.current"
      v-model:limit="queryParams.pageSize" @pagination="handleQuery" />
    <AudioEdit :dialog="dialog" :formData="formData" @resetQuery="resetQuery" :sourceList="sourceList" :deviceList="deviceList" :pptList="pptList" :commandList="commandList"  ></AudioEdit>
  </div>
</template>
<style lang="scss" scoped>
.app-container {
  .mb-4 {
    .data-box {
      background: white;
      height: 130px;
      display: flex;
      justify-content: space-between;
      padding: 20px;

      .data-box-info {
        .data-box-info-value {
          text-align: center;
        }

        .data-box-info-name {
          text-align: center;
        }

        .data-box-info-title {
          font-size: 20px;
          font-weight: bold;
          margin-bottom: 10px;
        }
      }
    }
  }
}
</style>